home *** CD-ROM | disk | FTP | other *** search
- Path: cypher.3do.com!user
- From: tsw@3do.com (Tom Watson)
- Newsgroups: comp.lang.c
- Subject: Re: Can you printf a long
- Date: Mon, 08 Apr 1996 13:02:16 -0800
- Organization: The 3DO Corporation
- Distribution: world
- Message-ID: <tsw-0804961302160001@cypher.3do.com>
- References: <4ju8o1$dc3@news.netam.net>
- NNTP-Posting-Host: cypher.3do.com
-
- In article <4ju8o1$dc3@news.netam.net>, bgc@alpha.netam.net (The Bowling
- Green Connection) wrote:
-
- > I had some trouble with this code:
- > (I'm using gcc on Digital Unix)
- >
- > int main() {
- > long i;
- >
- > while(1) {
- > i++;
- > printf("%f\n", i);
- > }
- >
- > return 0;
- > }
- >
- > The numbers printed to the screen as 0.00000.
- > (Nevermind that this is an infinite loop--I was piping the output to
- > a file which made me run out of disk space. This is just for testing)
- >
- > But when I changed %f to %d, the numbers printed correctly.
- > I thought that %d had the same limitations as an int..that is,
- > it could only print about 4 bits of information, whereas a long
- > is capable of more (8 or 16 bits), so wouldn't %f (float) be able
- > to better handle those long numbers? And why did %d work
- > correctly, even up to 634,000 (that's when my disk space ran out.. :))
- >
- > Well, then I changed the "long i" declaration to "int i", and changed %f
- > to %d, and I got the SAME results! The numbers went up to 634,000!
- > I didn't think an int could handle this much!
- >
- > Another strange thing is, I did a sizeof(int) and a sizeof(long), and they
- > BOTH returned a 4!!! That can't be right, can it??!
- >
-
- There are a couple of points to ponder here:
- 1) You are trying to print a 'long' with a '%f' specification. This is
- clearly not nice. The types must correspond.
- 2) The routine 'printf' is a routine that takes a variable number of
- arguments, and as such goes thru "the usual argument promotions" for your
- system. While it may not be significant in all cases (read:
- implementation defined), usually the argument is promoted to a 'double'
- and its size (again: Implementation defined) is probably 8.
- 3) In one machioe that I'm presently working on, arguments are passed
- thru registers. The floating point arguments in floating point registers,
- the integers in integer registers. The value may be fetched from the
- incorrect register (which has not been loaded in the case given).
- 4) If you desire to print out floating point numbers that have the same
- representation as small integers, you probably want to use a 'union'
- construct that will make the storage 'shared'.
- 5) If you have IEEE numbers, the floating point numbers with small
- integer representations are of the 'de-normalized' type.
- 6) If you want the structure of the numbers, it may be more meaningful to
- use a hex format.
- 7) The '%f' format may not have enough space to give you meaningful
- results, use a '%g' format which will give you an exponent for very small
- numbers.
-
- Lots of problems here!!
-
- --
- Tom Watson
- tsw@3do.com (Home: tsw@johana.com)
-